/**
 * Created by Administrator on 2017/1/13.
 */

module.exports = {
    build: function(k, v) {
        var obj = {};
        obj[k] = v;
        return obj;
    },

    /**
     * 取对象的值数组
     * @param obj
     * @returns {Array}
     */
    values: function(obj) {
        var vals = [];
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                vals.push(obj[key]);
            }
        }
        return vals;
    },

    /**
     * 遍历obj对象的成员
     * @param obj
     * @param callback(key, value) 返回true则退出遍历
     */
    each: function(obj, callback) {
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            var k = keys[i];
            if (callback(k, obj[k]) === false) break;
        }
    },

    /**
     * 遍历obj对象的成员，将key作为数字
     * @param obj
     * @param callback(key, value) 返回true则退出遍历
     */
    eachKeyNum: function(obj, callback) {
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            var k = keys[i];
            if (callback(+k, obj[k]) === false) break;
        }
    },

    /**
     * 将obj的key转换为数字数组
     * @param obj
     * @returns {Array}
     */
    numberKeys: function(obj) {
        var keys = Object.keys(obj);
        for (var i = 0, l = keys.length; i < l; ++i) {
            keys[i] = +(keys[i]);
        }
        return keys;
    },

    /**
     * 检查是否是对象
     * @param obj
     * @returns {boolean}
     */
    isObject: function(obj) {
        return (typeof(obj) == 'object');
    },

    isArray: function(obj) {
        return (obj instanceof Array);
    },

    isNumber: function(obj) {
        return isNaN(obj) ? false : true;
    },

    /**
     * 当obj的key的值当作数字 +value，如果没有该key则会自动添加
     * @param obj
     * @param key
     * @param value
     */
    addNumber: function(obj, key, value) {
        obj[key] = (+obj[key] || 0) + value;
    },

    /**
     * 当obj的key的值当作数字 +value，如果没有该key则会自动添加，最小值限定为0
     * @param obj
     * @param key
     * @param value
     */
    addNumber0: function(obj, key, value) {
        obj[key] = Math.max(0, (+obj[key] || 0) + value);
    },

    /**
     * 将obj的所有数字类型的值进行累加
     * @param obj
     * @returns {number}
     */
    sumValue: function(obj) {
        var sum = 0;
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            var value = obj[keys[i]];
            if (!isNaN(value)) {
                sum += (+value);
            }
        }
        return sum;
    },

    /**
     * 合并多个对象，新的字段则赋值，数字字段则相加
     * params 参数为需要进行合并的对象，比如merge(obj1, obj2, ...)
     * @returns {*}
     */
    merge: function() {
        var args = arguments;
        var obj = args[0];
        for (var i = 1; i < args.length; ++i) {
            this.each(args[i], function(k, v) {
                if (obj.hasOwnProperty(k)) {
                    if (!isNaN(obj[k]) && !isNaN(v)) {
                        obj[k] += v;
                    }
                } else {
                    obj[k] = v;
                }
            });
        }

        return obj;
    },

    /**
     * 将数值乘以value并四舍五入取整
     * @param obj
     * @param value
     */
    mul: function(obj, value, handler) {
        if (!handler) {
            handler = Math.round;
        }

        this.each(obj, function(k, v) {
            if (!isNaN(v)) {
                obj[k] = handler(v * value);
            }
        });
    },

    mulNew: function(obj, value, handler) {
        if (!handler) {
            handler = Math.round;
        }

        var newObj = {};
        this.each(obj, function(k, v) {
            if (!isNaN(v)) {
                newObj[k] = handler(v * value);
            }
        });

        return newObj;
    },


    /**
     * 将数值取负号
     * @param obj
     */
    minus: function(obj) {
        this.each(obj, function(k, v) {
            if (!isNaN(v)) {
                obj[k] = -v;
            }
        });
        return obj;
    },

    /**
     * 是否有大于value的元素
     * @param obj
     * @param value
     * @returns {boolean}
     */
    existGT: function(obj, value) {
        var exist = false;
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            if (obj[keys[i]] > value) {
                exist = true; break;
            }
        }
        return exist;
    },

    /**
     * 是否有大于等于value的元素
     * @param obj
     * @param value
     * @returns {boolean}
     */
    existGTE: function(obj, value) {
        var exist = false;
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            if (obj[keys[i]] >= value) {
                exist = true; break;
            }
        }
        return exist;
    },

    /**
     * 是否有小于value的元素
     * @param obj
     * @param value
     * @returns {boolean}
     */
    existLT: function(obj, value) {
        var exist = false;
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            if (obj[keys[i]] < value) {
                exist = true; break;
            }
        }
        return exist;
    },

    /**
     * 是否有小于等于value的元素
     * @param obj
     * @param value
     * @returns {boolean}
     */
    existLTE: function(obj, value) {
        var exist = false;
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            if (obj[keys[i]] <= value) {
                exist = true; break;
            }
        }
        return exist;
    },

    /**
     * 是否有等于value的元素
     * @param obj
     * @param value
     * @returns {boolean}
     */
    existEQ: function(obj, value) {
        var exist = false;
        var keys = Object.keys(obj);
        for (var i = 0, len = keys.length; i < len; ++i) {
            if (obj[keys[i]] == value) {
                exist = true; break;
            }
        }
        return exist;
    },

    /**
     * 统计元素数量
     * @param obj
     */
    count: function(obj) {
        return Object.keys(obj).length;
    },

    /**
     * 等于value的元素数量
     * @param obj
     * @param value
     */
    countEQ: function(obj, value) {
        var count = 0;
        this.each(obj, function(k ,v) {
            if (v == value) {
                count++;
            }
        })
        return count;
    },

    /**
     * 等于value的元素
     * @param obj
     * @param value
     */
    partEQ: function(obj, value) {
        var parts = {};
        this.each(obj, function(k ,v) {
            if (v == value) {
                parts[k] = v;
            }
        })
        return parts;
    },

    /**
     * 大于value的元素
     * @param obj
     * @param value
     */
    partGT: function(obj, value) {
        var parts = {};
        this.each(obj, function(k ,v) {
            if (v > value) {
                parts[k] = v;
            }
        })
        return parts;
    },

    /**
     * obj2的元素都是数字，obj1对应的元素是否都大于等于obj2中的元素
     * @param obj1
     * @param obj2
     * @constructor
     */
    numberGTE: function(obj1, obj2) {
        var result = true;
        var keys = Object.keys(obj2);
        for (var i = 0, len = keys.length; i < len; ++i) {
            if (isNaN(obj2[keys[i]]) || isNaN(obj1[keys[i]])) {
                result = false; break;
            }

            if (obj1[keys[i]] < obj2[keys[i]]) {
                result = false; break;
            }
        }

        return result;
    },

    /**
     * obj2的元素都是数字，obj1对应的元素是否都大于obj2中的元素
     * @param obj1
     * @param obj2
     * @constructor
     */
    numberGT: function(obj1, obj2) {
        var result = true;
        var keys = Object.keys(obj2);
        for (var i = 0, len = keys.length; i < len; ++i) {
            var key = keys[i];
            if (isNaN(obj2[key]) || isNaN(obj1[key])) {
                result = false; break;
            }

            if (obj1[key] <= obj2[key]) {
                result = false; break;
            }
        }

        return result;
    },

    /**
     * 克隆当前数组
     * @param arr
     */
    cloneArray: function (arr) {

        var result = [];
        if (!this.isArray(arr)) {
            return result;
        }

        for (var index = 0; index < arr.length; ++index) {
            result.push (clone (arr[index]));
        }

        return result;
    }
};